var layui_form = '';
var group_id   = 1;

layui.use(function(){
	var layer  = layui.layer;
	var util   = layui.util;
	layui_form = layui.form;
	
	getAgreement();
	getGroupEquity();
  
	layui_form.on('select(group_select-filter)', function(data){
		  var elem = data.elem; // 获得 select 原始 DOM 对象
		  var value = data.value; // 获得被选中的值
		  var othis = data.othis; // 获得 select 元素被替换后的 jQuery 对象
		  group_id = value;
		  getGroupEquity();
		  switch (value*1){
		  	case 1:
				$('.hide').hide();
		  		break;
			case 2:
				$('.hide').hide();
				$('#id_number').show();
				$('#real_name').show();
				break;
			case 3:
				$('.hide').hide();
				$('#id_number').show();
				$('#real_name').show();
				$('#company_name').show();
				$('#job_name').show();
				break;		
		  }
    });
	
	layui_form.on('submit(sub-validate)', function(data){
	    var field = data.field; // 获取表单字段值
		if(!wxx.regPhone(field.username)){layer.msg('请输入正确的手机号'); return false;}
		if(!field.password){layer.msg('请输入密码'); return false;}
		if(!field.group_id){layer.msg('请选择注册类型'); return false;}
		
		if(field.group_id == 2 || field.group_id == 3){
			if(!field.id_number){layer.msg('请输入身份证号'); return false;}
			if(!field.real_name){layer.msg('请输入姓名'); return false;}
		}
		if(field.group_id == 3){
			if(!field.company_name){layer.msg('请输入公司名称'); return false;}
			if(!field.job_name){layer.msg('请输入职位'); return false;}
		}
		if(!field.code){layer.msg('请输入手机验证码'); return false;}
		
		var loadIndex = layer.load(2);
		var datas = {
			url  :'/api/user/register',
			http_type:'POST',    
			datas:field
		}
		wxx.HttpReqeust(datas,function(res){
			layer.close(loadIndex)
			if(res.code == 1){
				layer.msg(res.msg)
				localStorage.setItem("jingjiang_userinfo",JSON.stringify(res.data.userinfo));
				localStorage.setItem("jingjiang_token",res.data.userinfo.token);
				setTimeout(function(){
					window.location.href='../../index.html'
				},800)
				
			}else{
				 layer.alert(res.msg,{title:'温馨提示'});
			}
		});
		
		
	    return false; // 阻止默认 form 跳转
	});
	
	
	 util.on('lay-on', {
		 
	    'service_agreement': function(){
	        layer.open({
			  type: 1,
			  title: ['服务协议', 'font-size:16px;font-weight: 600'],
			  area: ['920px', '620px'],
			  shade: true, // 显示遮罩
			  content: $('#hide_user_agreement'), // 捕获的元素
			  end: function(){
				// layer.msg('关闭后的回调', {icon:6});
			  }
			});
	    },
		'privacy_policy': function(){
		    layer.open({
			  type: 1,
			  title: ['隐私政策', 'font-size:16px;font-weight: 600'],
			  area: ['920px', '620px'],
			  shade: true, // 显示遮罩
			  content: $('#hide_user_privacy'), // 捕获的元素
			  end: function(){
				// layer.msg('关闭后的回调', {icon:6});
			  }
			});
		},
		'group': function(){
		    layer.open({
			  type: 1,
			  title: ['会员权益', 'font-size:16px;font-weight: 600'],
			  area: ['920px', '620px'],
			  shade: true, // 显示遮罩
			  content: $('#hide_user_group'), // 捕获的元素
			  end: function(){
				// layer.msg('关闭后的回调', {icon:6});
			  }
			});
		},
	})
  

});



function getAgreement(){
	var datas = {
		url  :'/api/user/agreement',
		http_type:'POST',    
		datas:{}
	}
	wxx.HttpReqeust(datas,function(res){
		if(res.code == 1){
			
			$('#hide_user_agreement .emuis').html(res.data.service_agreement)
			$('#hide_user_privacy .emuis2').html(res.data.privacy_policy)
			
		}
	});
}

function getGroupEquity(){
	var datas = {
		url  :'/api/user/getGroupEquity',
		http_type:'POST',    
		datas:{group_id:group_id}
	}
	wxx.HttpReqeust(datas,function(res){
		if(res.code == 1){
			$('#hide_user_group .emuis3').html(res.data.content)
		}
	});
}









